package com.lft.freshordering.service.impl;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lft.freshordering.entity.Gocar;
import com.lft.freshordering.entity.Goods;
import com.lft.freshordering.entity.OrderDetail;
import com.lft.freshordering.entity.Orders;
import com.lft.freshordering.entity.VO.CartShopVO;
import com.lft.freshordering.entity.Goods;
import com.lft.freshordering.entity.VO.CartShopVO;
import com.lft.freshordering.mapper.GocarMapper;
import com.lft.freshordering.mapper.GoodsMapper;
import com.lft.freshordering.mapper.OrderDetailMapper;
import com.lft.freshordering.mapper.OrdersMapper;
import com.lft.freshordering.mapper.GoodsMapper;
import com.lft.freshordering.service.IGocarService;

import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Service
public class GocarServicelmpl implements IGocarService {
    @Autowired
    private GocarMapper gocarMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    //新增购物车
    @Override
    public String insertGocar(Gocar gocar) {
        //根据用户id和商品id 查询购物车中的商品
        Gocar gocar1 = gocarMapper.selectGocar(gocar.getUid(), gocar.getSid());

        if (gocar1 != null) {
            //如果不为空说明购物车中存在此商品 更新其个数量
            Integer count1 = gocar1.getCount();//获取数据库中的数据
            Integer count = gocar.getCount();//传来的数量
            int sum = count + count1;

            gocar.setCount(sum);//存入实体类中
            int i = gocarMapper.updateGocar(gocar);
            if (i > 0) {
                return "数量已添加";

            } else {
                return "添加失败";
            }
        } else {
            //不为空说明不存在此商品，直接添加购物车
            int i = gocarMapper.insertGocar(gocar);
            if (i > 0) {
                return "已添加购物车";
            } else {
                return "添加失败";
            }
        }
    }

    //根据用户id查询购物车信息
    @Override
    public List<CartShopVO> selectbyuid(Integer uid) {
        List<CartShopVO> cartShopVOS = new ArrayList<>();
        //查询到用户的购物车信息
        List<Gocar> selectbyuid = gocarMapper.selectbyuid(uid);
        for (Gocar gocar : selectbyuid) {//遍历购物车信息集合
            CartShopVO cartShopVO = new CartShopVO();//购物车中的商品的VO类
            Integer sid = gocar.getSid();//获取到商品id

            Goods goods = goodsMapper.selectByid(sid);//根据商品id查询回来的商品

            cartShopVO.setSid(goods.getGoodsId());
            cartShopVO.setPrice(goods.getGjprices());//商品价格
            cartShopVO.setShopname(goods.getGoodsName());//购物车商品名字
            cartShopVO.setCount(gocar.getCount());//购物车商品数量

            cartShopVOS.add(cartShopVO);
        }

        return cartShopVOS;
    }

    //用户下单操作
    @Transactional
    @Override
    public void addzhangdan(Map map) {
        Orders orders = new Orders();//新增的订单对象

        Integer uid = (Integer) map.get("uid");//用户id

        List shoplist = (List) map.get("shoplist");//账单的商品集合

        Integer zongjia = (Integer) map.get("zongjie");
        //map集合拿出来的是Object类型的数据,以下方法是对其进行强制类型转换
//        ObjectMapper objectMapper = new ObjectMapper();
//        Double zdprice = objectMapper.convertValue(zongjia, new TypeReference<Double>() {
//        });//账单总价

        StringBuffer stringBuffer = new StringBuffer();//订单的编号
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            int sui = random.nextInt(9) + 1;//生成1-9之间的随机数
            stringBuffer.append(sui);//字符串追加
        }
        String string = stringBuffer.toString();
        orders.setId(string);//订单编号
        orders.setPrice(zongjia);//账单总价
        orders.setUid(uid);//订单所有人
        //新增订单
        ordersMapper.insertneworders(orders);

        for (int i = 0; i < shoplist.size(); i++) {//遍历账单商品集合
            OrderDetail orderDetail = new OrderDetail();
            Map map1 = (Map) shoplist.get(i);//将集合中的元素转换成map集合

            Integer sid = (Integer) map1.get("sid");//商品id
            Integer count = (Integer) map1.get("count");//商品数量

            orderDetail.setSid(sid);
            orderDetail.setOid(string);
            orderDetail.setCount(count);

            //新增账单详情商品
            orderDetailMapper.addorderDetail(orderDetail);

            //下单后删除购物车中的商品
            goodsMapper.deletebyids(uid, sid);
        }


    }

    //用户删除购物车中的商品
    @Override
    public Integer deletebyids(Map map) {
        Integer uid = (Integer) map.get("uid");//用户id
        Integer sid = (Integer) map.get("sid");//商品id

        Integer i = goodsMapper.deletebyids(uid, sid);

        return i;
    }


}
